package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.annotation.AutoFill;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.enumeration.OperationType;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface SetmealMapper {

    /**
     * 根据分类id查询套餐的数量
     * @param id
     * @return
     */
    @Select("select count(id) from setmeal where category_id = #{categoryId}")
    Integer countByCategoryId(Long id);

    /**
     * 套餐分页查询
     * @param dto
     * @return
     */
    Page<SetmealVO> page(SetmealPageQueryDTO dto);

    /**
     * 添加套餐
     * @param setmeal
     */
    @AutoFill(OperationType.INSERT)
    @Options(keyProperty = "id",useGeneratedKeys = true)
    @Insert("insert into sky_take_out.setmeal values (null,#{categoryId},#{name},#{price},#{status},#{description},#{image},#{createTime},#{updateTime},#{createUser},#{updateUser})")
    void add(Setmeal setmeal);

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Select("select * from sky_take_out.setmeal where id =#{id}")
    SetmealVO findById(Long id);

    /**
     * 修改套餐
     * @param setmeal
     */
    @Update("update sky_take_out.setmeal set category_id=#{categoryId},name=#{name},price=#{price},status=#{status},description=#{description},image=#{image} where id= #{id}")
    void update(Setmeal setmeal);

    /**
     * 套餐起售、停售
     * @param status
     * @param id
     */
    @Update("update sky_take_out.setmeal set status=#{status} where id=#{id}")
    void updateStatus(Integer status,Long id);

    /**
     * 根据id删除套餐
     * @param id
     */
    @Delete("delete from sky_take_out.setmeal where id=#{id}")
    void deleteQuery(Long id);

    /**
     * 动态条件查询套餐
     * @param setmeal
     * @return
     */
    List<Setmeal> list(Setmeal setmeal);

    /**
     * 根据套餐id查询菜品选项
     * @param setmealId
     * @return
     */
    @Select("select sd.name, sd.copies, d.image, d.description " +
            "from setmeal_dish sd left join dish d on sd.dish_id = d.id " +
            "where sd.setmeal_id = #{setmealId}")
    List<DishItemVO> getDishItemBySetmealId(Long setmealId);

    /**
     * 根据条件统计套餐数量
     * @param map
     * @return
     */
    Integer countByMap(Map map);
}
